package com.taotao.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.EUTreeNode;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.mapper.TbContentCategoryMapper;
import com.taotao.pojo.TbContentCategory;
import com.taotao.pojo.TbContentCategoryExample;
import com.taotao.pojo.TbContentCategoryExample.Criteria;
@Service
public class ContentCatServiceImpl implements ContentCatService {
	@Autowired
	private TbContentCategoryMapper contentCatMapper;
	
	@Override
	public List<EUTreeNode> getContentCat(long parentId) {
		// TODO Auto-generated method stub
		TbContentCategoryExample example = new TbContentCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(parentId);
		List<TbContentCategory> list = contentCatMapper.selectByExample(example);
		List<EUTreeNode> result = new ArrayList<EUTreeNode>();
		
		for (TbContentCategory contentCat : list) {
			EUTreeNode treeNode = new EUTreeNode();
			treeNode.setId(contentCat.getId());
			treeNode.setText(contentCat.getName());
			treeNode.setState(contentCat.getIsParent()?"closed":"open");
			result.add(treeNode);
		}
		return result;
	}
	
	@Override
	public TaotaoResult createContent(long parentId, String name) {
		// TODO Auto-generated method stub
		TbContentCategory contentCat = new TbContentCategory();
		contentCat.setParentId(parentId);
		contentCat.setName(name);
		contentCat.setSortOrder(1);
		contentCat.setIsParent(false);
		contentCat.setCreated(new Date());
		contentCat.setUpdated(new Date());
		contentCat.setStatus(1);
		TbContentCategory parentCat = contentCatMapper.selectByPrimaryKey(parentId);
		if (!parentCat.getIsParent()) {
			parentCat.setIsParent(true);
			contentCatMapper.updateByPrimaryKey(parentCat);
		}
		contentCatMapper.insert(contentCat);
		
		//System.out.println(insertStat);
		return TaotaoResult.ok(contentCat);
	}

	@Override
	public TaotaoResult updateContent(long id, String name) {
		// TODO Auto-generated method stub
		TbContentCategory result = new TbContentCategory();
		result.setId(id);
		result.setName(name);
		result.setUpdated(new Date());
		int updateStat = contentCatMapper.updateByPrimaryKeySelective(result);
		//System.out.println(updateStat);
		if (updateStat > 0) {
			return TaotaoResult.ok(result);
		}
		return TaotaoResult.ok();
	}

	@Override
	public TaotaoResult deleteContent(long id) {;
		// TODO Auto-generated method stub
		TbContentCategory selectCon = contentCatMapper.selectByPrimaryKey(id);
		long parentId = selectCon.getParentId();
		TbContentCategory parentCon = contentCatMapper.selectByPrimaryKey(parentId);
		deleteNowAndChildNode(id);
		if (!hasChildNode(parentId)) {
			parentCon.setIsParent(false);
			contentCatMapper.updateByPrimaryKey(parentCon);
		}
		return TaotaoResult.ok();
	}
	
	private void deleteNowAndChildNode(long id) {
		TbContentCategory selectCon = contentCatMapper.selectByPrimaryKey(id);
		//如果当前节点是父节点，则选出所有子节点
		if (selectCon.getIsParent()) {
			TbContentCategoryExample example = new TbContentCategoryExample();
			Criteria criteria = example.createCriteria();
			criteria.andParentIdEqualTo(id);
			List<TbContentCategory> childList = contentCatMapper.selectByExample(example);
			for (TbContentCategory child : childList) {
				deleteNowAndChildNode(child.getId());
			}
			//子节点删除完成之后，本节点也要删除
			contentCatMapper.deleteByPrimaryKey(id);
			
		} else {
			contentCatMapper.deleteByPrimaryKey(id);
		}
	}
	
	private boolean hasChildNode(long id) {
		TbContentCategoryExample example = new TbContentCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(id);
		List<TbContentCategory> childList = contentCatMapper.selectByExample(example);
		if (childList.size() == 0) {
			return false;
		}
		return true;
	}


}
